﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UbbTimetable2
{
    /// <summary>
    /// A form for scheduling and unscheduling classes.
    /// </summary>
    public partial class SchedForm : Form
    {
        private MainForm _parent;
        private ClassesDao _classesDao;
        private TeachingDao _teachingDao;

        /// <summary>
        /// Main constructor.
        /// </summary>
        /// <param name="parent">The parent MainForm that will be frozen.</param>
        public SchedForm(MainForm parent)
        {
            InitializeComponent();

            _parent = parent;
            _parent.Enabled = false;
            _classesDao = new ClassesDao();
            _teachingDao = new TeachingDao();

            LoadAll();
        }

        private void SchedForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _parent.Enabled = true;
        }

        /// <summary>
        /// Fill up all combos with data
        /// </summary>
        private void LoadAll()
        {
            // Fill the combo of the days
            _parent.LoadDayCb(_parent.DayDal.GetDayList(), cbDay);
            // Fill the combo of subgroups
            _parent.LoadSubgroupCb(_parent.SubgroupDal.GetSubgroupList("any"), cbSubgroup);
            // Fill the combo of classrooms
            _parent.LoadClassroomCb(_parent.ClassroomDal.GetClassroomList(0), cbClassroom);

            Utilities.FillDgv(_classesDao, dgvClasses, Utilities.CLASSES_UNSCHEDULED);
            Utilities.FillDgv(_teachingDao, dgvTeaching);
        }

        private void btSchedule_Click(object sender, EventArgs e)
        {
            if (!CorrectInput())
            {
                MessageBox.Show("The input is not correct!");
            }
            else if (dgvClasses.SelectedRows.Count == 1)
            {
                _teachingDao.ScheduleClass(Convert.ToInt32(dgvClasses.SelectedRows[0].Cells[0].Value),
                    Convert.ToInt32(cbDay.SelectedValue), Convert.ToInt32(cbClassroom.SelectedValue),
                    Convert.ToInt32(cbSubgroup.SelectedValue), Convert.ToInt32(tbStartTime.Text),
                    Convert.ToInt32(tbEndTime.Text));
                if (_teachingDao.Err != "OK")
                {
                    MessageBox.Show("Nem lehet a kivalasztott parameterekkel megtartani az orat!");
                }
                else
                {

                    Utilities.FillDgv(_classesDao, dgvClasses, Utilities.CLASSES_UNSCHEDULED);
                    Utilities.FillDgv(_teachingDao, dgvTeaching);
                }
            }
        }

        private void btUnchedule_Click(object sender, EventArgs e)
        {
            if (dgvTeaching.SelectedRows.Count == 1)
            {
                _teachingDao.DeleteTeaching(Convert.ToInt32(dgvTeaching.SelectedRows[0].Cells[7].Value),
                    Convert.ToInt32(dgvTeaching.SelectedRows[0].Cells[9].Value),
                    Convert.ToInt32(dgvTeaching.SelectedRows[0].Cells[10].Value),
                    Convert.ToInt32(dgvTeaching.SelectedRows[0].Cells[8].Value));

                Utilities.FillDgv(_classesDao, dgvClasses, Utilities.CLASSES_UNSCHEDULED);
                Utilities.FillDgv(_teachingDao, dgvTeaching);
            }
        }

        /// <summary>
        /// Tells if the input textboxes have correct values.
        /// </summary>
        /// <returns>True if the input is correct and false otherwise.</returns>
        private bool CorrectInput()
        {
            int t1, t2;
            try
            {
                t1 = Convert.ToInt32(tbStartTime.Text);
                t2 = Convert.ToInt32(tbEndTime.Text);
            }
            catch (FormatException)
            {
                return false;
            }
            return Utilities.IsTime(t1) && Utilities.IsTime(t2) && t1 < t2;
        }
    }
}
